/* eslint valid-jsdoc: "off" */

'use strict';

/**
 * @param {Egg.EggAppInfo} appInfo app info
 */
module.exports = appInfo => {
  /**
   * built-in config
   * @type {Egg.EggAppConfig}
   **/
  const config = exports = {};

  // use for cookie sign key, should change to your own and keep security
  config.keys = appInfo.name + '_1617244589988_5558';

  // add your middleware config here 中间件执行顺序是数组中的顺序
  config.middleware = [ 'auth', 'stats' ];

  // 配置默认的bodyParser； egg默认对body请求解析的时候门限值body最大长度100kb；（解析成功之后是一个Object，也可能是个数组）
  config.bodyParser = {
    jsonLimit: '1mb',
    formLimit: '1mb',
  }


  // token校验中间件
  config.auth = {
    enable: false,  // 暂时关闭权限校验 
    // 配置token校验白名单
    ignore(ctx) {
      const regAuth = /^\/api\/auth\/login/i;
      return regAuth.test(ctx.url);
    }
  }

  // 测试统计中间件 = 只统计/members/getToken
  config.stats = {
    enable: true,
    match(ctx) {
      const reg = /^\/members\/getToken/i;
      return reg.test(ctx.url)
    }
  }

  // 测试错误码？？？
  config.errorCode = {
    'NO_TOKEN': 'token不存在',
    'UNVALIABLE_TOKEN': 'TOEKN已失效'
  }

  config.sequelize = {
    dialect: 'mysql',
    // host: '127.0.0.1',
    host: 'localhost',
    port: 3306,
    database: 'manyou',
    username: 'root',
    password: '321321',
    delegate: 'model',
  };

  // 修改默认服务器配置
  config.cluster = {
    listen: {
      path: '',
      port: 9527,
      hostname: '127.0.0.1'
    }
  }

  config.security = {
    xframe: {
      enable: false,
    },
    csrf: {
      enable: false, // 暂时关闭安全插件; 前后端分离， post请求不方便携带 _csrf
      ignoreJSON: true,
    },
    domainWhiteList: ['http://localhost:8080'], //配置白名单
  };

  config.cors = {
    // origin: '*',  // 允许所有跨域访问，如果注释则允许上面配置的 白名单 访问
    credentials: true,  // 允许跨域请求携带cookies
    allowMethods: 'GET,HEAD,PUT,POST,DELETE,PATCH'
  }

  config.multipart = {
    mode: 'file', // file模式上传stream处理报错：the multipart request can't be consumed twice
    // mode: 'stream',
  };

  // jwt tplen 登录鉴权
  config.jwt = {
    secret: '321321'
  }

  // redis存储管理用户token
  config.redis = {
    client: {
      port: 6379,
      host: '127.0.0.1',
      password: null,
      db: 0,
    }
    /* Multi Clients
     clients: {
      foo: {                 // instanceName. See below
        port: 6379,          // Redis port
        host: '127.0.0.1',   // Redis host
        password: 'auth',
        db: 0,
      },
      bar: {
        port: 6379,
        host: '127.0.0.1',
        password: 'auth',
        db: 1,
      },
    }
     */
  }

  // add your user config here
  const userConfig = {
    myAppName: '慢游',
    intro: '飘啊飘啊飘啊飘，没听到没看到也不想知道。'
  };

  return {
    ...config,
    ...userConfig,
  };
};
